package com.rachel.apkupdate;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodSession;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rachel.apkupdate.ToolUtils.TypeConverUtils;
import com.rachel.apkupdate.ToolUtils.log;
import com.rachel.apkupdate.entris.FileInfo;
import com.rachel.apkupdate.presenter.UpdatePresenter;
import com.rachel.apkupdate.view.IDownloadView;

import java.io.File;

import static android.os.Environment.DIRECTORY_DCIM;
import static android.os.Environment.DIRECTORY_DOWNLOADS;

public class MainActivity extends Activity implements IDownloadView{
	private static final String TAG = "zsr";
	private CheckVersionManager versionManager;
	UpdatePresenter mUpdatePresenter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		final View rootview = LayoutInflater.from(this).inflate(R.layout.activity_main,null);
		versionManager = CheckVersionManager.getInstance(this, new VersionCallback() {
			@Override
			public void update(FileInfo fileinfo) {
				showPopupwindow(rootview,fileinfo);

			}

			@Override
			public void lastest() {

			}
		});

		mUpdatePresenter = UpdatePresenter.getInstance(this);

		/*log.d("path2: "+ Environment.getDownloadCacheDirectory()+" sdf: "+Environment.getDataDirectory()
		+"2: "+ Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS));
		String dir = Environment.getDownloadCacheDirectory()+"/mydownload/";
		File file = new File(dir);
		if (!file.exists()){
			file.mkdir();
			log.d("file: "+file.getPath());
		}*/
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		versionManager.Destroy();
	}



	public void showPopupwindow(View rootView, final FileInfo fileInfo) {
		View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.apkupdatewindow, null);
		final PopupWindow popupWindow = new PopupWindow(view,
				ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
		popupWindow.setAnimationStyle(R.style.popwin_anim_style);
		TextView textVersion = (TextView) view.findViewById(R.id.app_version);
		TextView textMsg = (TextView) view.findViewById(R.id.app_updatemsg);
		TextView textsize = (TextView) view.findViewById(R.id.app_size);


		textVersion.setText(getResources().getString(R.string.appversion) + fileInfo.getVersionname());
		long filesize =  fileInfo.getFilesize();

		textsize.setText(getResources().getString(R.string.appsize) +
				TypeConverUtils.getFloatSize(filesize));


		textMsg.setText(fileInfo.getVersionmsg());


		view.findViewById(R.id.sure).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				popupWindow.dismiss();
				mUpdatePresenter.startDownload(fileInfo);
				showFloatWindow();

			}
		});
		view.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				popupWindow.dismiss();
			}
		});
		popupWindow.showAtLocation(rootView, Gravity.CENTER, 0, 0);



	}

	public void window(View view){
		versionManager.test();
	}


	public void showFloatWindow(){
		// 通过service类获取实例
		WindowManager wManager = (WindowManager) MyAppLiCation.getContext().getSystemService(Context.WINDOW_SERVICE);

		WindowManager.LayoutParams params = new WindowManager.LayoutParams();

		//设置type类型
		params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; //这个比 TYPE_PHONE 优先级要高

		//设置背景图片
		params.format = PixelFormat.RGBA_8888; //透明效果

		//设置标志,这个两个标志为了让就算出现了悬浮窗，不影响其他区域，只在悬浮窗的区域受干扰
		params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|
				WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

		//设置大小
		params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
		params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
		params.gravity = Gravity.BOTTOM | Gravity.RIGHT;
		//设置悬浮窗的布局
		LayoutInflater inflater = LayoutInflater.from(MyAppLiCation.getContext());  //获取共享全局布局
		RelativeLayout mLayout = (RelativeLayout) inflater.inflate(R.layout.filedownload, null); // 这里的浮窗口定义成圆球
		mProgressBar = (ProgressBar) mLayout.findViewById(R.id.progressBar);
		mProgressText = (TextView) mLayout.findViewById(R.id.progresstext);
		mDownloadsize = (TextView) mLayout.findViewById(R.id.downloadsize);
		mFilesize = (TextView) mLayout.findViewById(R.id.filesize);
		wManager.addView(mLayout, params); //把配置好的视图添加进去
	};

	private ProgressBar mProgressBar;
	private TextView mProgressText,mDownloadsize,mFilesize;
	@Override
	public void updateProgress(int progress) {
		mProgressBar.setProgress(progress);
		mProgressText.setText(progress+"%");
	}

	@Override
	public void setDownloadSize(String downloadSize) {
		mDownloadsize.setText(downloadSize);
	}

	@Override
	public void setFileSize(String fileSize) {
		mFilesize.setText(fileSize);
	}

}
